package com.example.basefream.common;

import com.example.basefream.util.ExcelUtil;

import java.io.File;
import java.util.List;

public class GeneratorFileService {

        /**
         * 相对路径，文件会生成在与项目平级的目录
         */
        private final String FILE_PATH = "data/excel";

        private final String EXCEL_SUFFIX = ".xlsx";

        /**
         * 生成excel文件
         *
         * @param fileName
         * @param clazz
         * @param list
         * @param <T>
         * @return
         */
        public <T> File generatorExcel(String fileName, Class<T> clazz, List<T> list) {
            if (generateFilePath()) {
                return null;
            }
            String fileFullName = FILE_PATH + fileName + EXCEL_SUFFIX;

            ExcelUtil.writeToExcel(fileFullName, clazz, list);
            return new File(fileFullName);
        }

        /**
         * 删除文件
         *
         * @param file
         * @return
         */
        public boolean deleteFile(File file) {
            if (file != null) {
                return file.delete();
            }
            return true;
        }

        private boolean generateFilePath() {
            File path = new File(FILE_PATH);
            if (path.exists()) {
                return true;
            } else {
                return path.mkdirs();
            }
        }
}

